﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Linq.Expressions;

namespace BusinessLogic
{
    public class StylistLogic
    {

        public List<dynamic> getPageDate<T, TKey>(Expression<Func<T, dynamic>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int Total)
                   where T : class
        {
            Model1Container db = new Model1Container();
            Total = db.Set<T>().Where(where).Count();
            var list = db.Set<T>().Where(where).OrderByDescending(order).Select(select).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list.ToList();
        }

        public Stylist GetForm(int keyValue)
        {
            Model1Container db = new Model1Container();
            return db.Stylist.Find(keyValue);
        }
        public List<PriceConfig> GetPriceConfig()
        {

            Model1Container db = new Model1Container();
            return db.PriceConfig.ToList();
        }
        public List<StylistPrice> GetStylistPrice(string UserID)
        {
            Model1Container db = new Model1Container();
            return db.StylistPrice.Where(x => x.UserID == UserID).ToList();
        }
        public V_Stylist GetForm_ShopName(int keyValue)
        {
            Model1Container db = new Model1Container();
            return db.V_Stylist.Find(keyValue);
        }

        public Stylist GetStylistModel(int keyValue)
        {
            Model1Container db = new Model1Container();
            return db.Stylist.Find(keyValue);
        }
        public int AddInfo(Stylist model)
        {
            int result = 0;
            try
            {
                Model1Container db = new Model1Container();
                db.Stylist.Add(model);
                db.SaveChanges();
                result = model.ID;
            }
            catch (Exception ex)
            {
                return 0;
            }
            return result;
        }

        public int AddStylistPrice(string UserID,List<StylistPrice> list)
        {
            int result = 0;
            try
            {
                Model1Container db = new Model1Container();
                db.StylistPrice.RemoveRange(db.StylistPrice.Where(x=>x.UserID==UserID).ToList());

                for (int i = 0; i < list.Count; i++)
                {
                    list[i].UserID = UserID;
                    db.StylistPrice.Add(list[i]);
                }

                db.SaveChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return 0;
            }
            return result;
        }
        public int UpdateInfo(Stylist model)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var newModel = db.Stylist.Where(c => c.ID == model.ID).First();
                    newModel.Birthday = model.Birthday;
                    newModel.Sex = model.Sex;
                    newModel.Address = model.Address;
                    newModel.Price = model.Price;
                    newModel.ShopID = model.ShopID;
                    newModel.Name = model.Name;
                    newModel.NickName = model.NickName;
                    newModel.Height = model.Height;
                    newModel.Weight = model.Weight;
                    newModel.Introduce = model.Introduce;
                    //newModel.Addtime = model.Addtime;
                    //newModel.AddPerson = model.AddPerson;
                    newModel.ModifyDate = model.ModifyDate;
                    newModel.ModifyPerson = model.ModifyPerson;
                    newModel.Pic = model.Pic;
                    newModel.Tel = model.Tel;
                   // newModel.Isdelete = model.Isdelete;
                    newModel.IsShow = model.IsShow;

                    newModel.OrderIndex = model.OrderIndex;
                    newModel.IDCard = model.IDCard;
                    newModel.WorkDay = model.WorkDay;
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
        public int DelInfo(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.Stylist.First(p => p.ID == id);
                    model.Isdelete = 1;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
        public int Show(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.Stylist.First(p => p.ID == id);
                    model.IsShow = 1;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
        public int UnShow(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.Stylist.First(p => p.ID == id);
                    model.IsShow = 0;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
      

        public int StylelistDetailsInsert(List<StylelistDetailsInfo> list)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    string guid = list[0].PersonGuid;
                    var temlist = db.StylelistDetailsInfo.Where(c => c.PersonGuid == guid).ToList();
                    db.StylelistDetailsInfo.RemoveRange(temlist);
                    db.SaveChanges();
                }
                using (Model1Container db = new Model1Container())
                {
                    var model = db.StylelistDetailsInfo.AddRange(list);
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public List<StylelistDetailsInfo> GetStylelistDetailsInfoList(string keyValue)
        {
            List<StylelistDetailsInfo> resultlist = new List<StylelistDetailsInfo>();
            resultlist.Add(new StylelistDetailsInfo());
            if (string.IsNullOrEmpty(keyValue))
            {
                return resultlist;
            }
            try
            {
                Model1Container db = new Model1Container();
                var result=db.StylelistDetailsInfo.Where(x => x.PersonGuid == keyValue).ToList();
                if (result != null && result.Count > 0)
                {
                    return result;
                }
            }
            catch (Exception ex)
            {
                return resultlist;
            }
            return resultlist;
        }
    }
}
